<template>
  <div class="hello">
    <div>===================这里是组件2 start==================</div>
    组件1状态机==={{ key1 }}
    <button @click="handleAdd">更新key2</button>
    <div>===================这里是组件2 end==================</div>
  </div>
</template>

<script>
import { sharedState, subscribe } from "@/utils/SharedState";
export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
  data() {
    return {
      key1: 0,
    };
  },
  created() {
    // this.key1 = sharedState.key1;
    sharedState.key2 = [0];
    subscribe("key1", () => {
      console.log("key1更新了", sharedState);
      // this.key1 = sharedState.key1;
    });
  },
  methods: {
    handleAdd() {
      sharedState.key2.push(1);
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
